prepare("SELECT * FROM exam_sessions WHERE student_id = ? AND exam_id = ? AND status = 'in_progress'"); $stmt->execute([$_SESSION['student_id'], $exam_id]); $session = $stmt->fetch(PDO::FETCH_ASSOC); if (!$session) { // Create new session $start_time = date('Y-m-d H:i:s'); $end_time = date('Y-m-d H:i:s', strtotime("+{$exam['duration']} minutes")); $time_remaining = $exam['duration'] * 60; $stmt = $pdo->prepare("INSERT INTO exam_sessions (student_id, exam_id, start_time, end_time, time_remaining) VALUES (?, ?, ?, ?, ?)"); $stmt->execute([$_SESSION['student_id'], $exam_id, $start_time, $end_time, $time_remaining]); $session_id = $pdo->lastInsertId(); } else { $session_id = $session['id']; $time_remaining = $session['time_remaining']; } // Get questions for this exam $stmt = $pdo->prepare("SELECT * FROM questions WHERE exam_id = ? ORDER BY id"); $stmt->execute([$exam_id]); $questions = $stmt->fetchAll(PDO::FETCH_ASSOC); // Handle question navigation $current_page = $_GET['page'] ?? 1; $questions_per_page = 5; $total_questions = count($questions); $total_pages = ceil($total_questions / $questions_per_page); // Calculate question range for current page $start_index = ($current_page - 1) * $questions_per_page; $end_index = min($start_index + $questions_per_page, $total_questions) - 1; // Handle answer submission if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['submit_answers'])) { foreach ($_POST['answers'] as $question_id => $selected_answer) { // Check if answer already exists $stmt = $pdo->prepare("SELECT id FROM student_answers WHERE session_id = ? AND question_id = ?"); $stmt->execute([$session_id, $question_id]); if ($stmt->rowCount() > 0) { // Update existing answer $stmt = $pdo->prepare("UPDATE student_answers SET selected_answer = ?, is_correct = (SELECT correct_answer = ? FROM questions WHERE id = ?) WHERE session_id = ? AND question_id = ?"); $stmt->execute([$selected_answer, $selected_answer, $question_id, $session_id, $question_id]); } else { // Insert new answer $stmt = $pdo->prepare("INSERT INTO student_answers (session_id, question_id, selected_answer, is_correct) VALUES (?, ?, ?, (SELECT correct_answer = ? FROM questions WHERE id = ?))"); $stmt->execute([$session_id, $question_id, $selected_answer, $selected_answer, $question_id]); } } // Update time remaining $time_remaining = $_POST['time_remaining']; $stmt = $pdo->prepare("UPDATE exam_sessions SET time_remaining = ? WHERE id = ?"); $stmt->execute([$time_remaining, $session_id]); // Redirect to next page or results if ($current_page < $total_pages) { redirect("exam.php?exam_id=$exam_id&page=" . ($current_page + 1)); } else { // Submit exam $stmt = $pdo->prepare("UPDATE exam_sessions SET status = 'completed', time_remaining = 0 WHERE id = ?"); $stmt->execute([$session_id]); // Calculate and save result $correct_answers = calculate_score($session_id, $pdo); $percentage = ($correct_answers / $total_questions) * 100; $stmt = $pdo->prepare("INSERT INTO results (session_id, total_questions, questions_attempted, correct_answers, score, percentage) VALUES (?, ?, ?, ?, ?, ?)"); $stmt->execute([$session_id, $total_questions, $total_questions, $correct_answers, $correct_answers, $percentage]); redirect("result.php?session_id=$session_id"); } } ?> Exam - <?php echo $exam['name']; ?>

()

Subject:

Time Remaining:

Question

( mark 1 ? 's' : ''; ?>)